﻿using System.Collections.Generic;

namespace KeyboardBubble
{
    public class WinManager
    {
        private static WinManager _instance;
        public static WinManager Instance
        {
            get
            {
                if (_instance == null) _instance = new WinManager();
                return _instance;
            }
        }

        public List<KeyMessageBox> winList;
        public WinManager()
        {
            winList = new List<KeyMessageBox>();
        }

        public void AddWin(string key, string note = "")
        {
            var win = new KeyMessageBox(key, note);
            var top = (string.IsNullOrWhiteSpace(win.lableNote.Text) ? 41 : 64) + 30;
            // 如果已经存在窗体，之前的窗体需要向上移动
            // 大64   小41
            if (winList.Count > 0)
            {
                for (int i = winList.Count - 1; i >= 0; i--)
                {
                    var item = winList[i];
                    if (string.IsNullOrWhiteSpace(item.lableNote.Text))
                    {
                        top += 41;
                    }
                    else
                    {
                        top += 64;
                    }
                    item.SetBottom(top + ((winList.Count - i) * 10));
                }
            }
            // 如果窗体数量超过10个，将第第六个（包括）之后的窗体计时器停止并立即执行隐藏
            if (winList.Count > 5)
            {
                for (int i = 0; i < winList.Count - 5; i++)
                {
                    winList[i].StartFadeOut(null, null);
                }
            }
            winList.Add(win);
            win.Show();
        }

        public void removeObject(KeyMessageBox box)
        {
            this.winList.Remove(box);
        }
    }
}
